package com.zhehekeji.opsys.api.service.manager;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import org.apache.cxf.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.zhehekeji.opsys.api.common.util.ResultCode;
import com.zhehekeji.opsys.api.resp.StatisticalTrafficResp;
import com.zhehekeji.opsys.api.service.CommonService;

/**
 * @ClassName DataSatisticsService
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2015-3-26   上午10:54:59
 */
@Transactional
public class DataSatisticsService extends CommonService implements IDataSatisticsService {

	private final static Logger logger = LoggerFactory.getLogger(DataSatisticsService.class);
	
	/**(非 Javadoc) 
	* <p>Title: statisticalTraffic</p> 
	* <p>Description: </p> 
	* @param productCode
	* @return 
	* @see IDataSatisticsService#statisticalTraffic(java.lang.String)
	*/
	@Override
	public StatisticalTrafficResp statisticalTraffic(String productCode) {
		
		StatisticalTrafficResp resp = new StatisticalTrafficResp();
		if (StringUtils.isEmpty(productCode)){
			logger.error("统计产品访问量失败，产品编码非法！");
			resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("统计产品访问量失败，产品编码非法！");
			return resp;
		}
		try {
			
			List<Map<String, Object>> listMap = entityDao.getScrollDataBySql("select count(o.ID) as count from t_visit_log o where o.PRODUCT_CODE=?", new Object[]{productCode}, -1, -1);
			Long count = 0L;
			if (listMap != null && !listMap.isEmpty()){
				Map<String, Object> map = listMap.get(0);
				BigInteger tmpCount = (BigInteger) map.get("count");
				if (tmpCount != null){
					count = tmpCount.longValue();
				}
			}
			resp.setTraffic(count);
			logger.info("统计产品访问量成功");
		} catch (Exception e) {
			logger.error("统计产品访问量失败，系统异常，" + e.getMessage(), e);
			resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("统计产品访问量失败，系统异常");
		}
		return resp;
	}
}
